package com.honey.mybatis.generator.criteria.method;


import com.honey.compilation.java.Clazz;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.Parameter;
import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.IndentSpace;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.JDKFullyQualifiedJavaType;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.criteria.CriteriaConstant;

public class AndLikeMethod extends BaseMethod {

	private static final String METHOD_NAME = "andLike" ;
	
	private static final String[] JAVA_DOC = new String []{
		"使用and连接 like 条件(select update delete)。"
	};
	
	private static final  Parameter FIRST_PARAMETER = new Parameter(JDKFullyQualifiedJavaType.getObjectInstance(),"value");
	
	
	public AndLikeMethod(){
	}
	public AndLikeMethod(Clazz clazz, MybatisPlugin belongPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
	}
		
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodName()
	 */
	@Override
	protected String getMethodName() {
		return METHOD_NAME;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getJavaDoc()
	 */
	@Override
	protected String[] getJavaDoc() {
		return JAVA_DOC;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.criteria.method.BaseMethod#getFirstParameter()
	 */
	@Override
	protected Parameter getFirstParameter() {
		return FIRST_PARAMETER;
	}
	
	@Override
	protected void addBody(Method method) {
//		CharacterBuilder cb =CharacterBuilderFactory.createDefaultStringBuilder();
//		cb.append(CriteriaConstant.ADD_CRITERIA_METHOD).append("(");IndentSpace.newSpace(cb, 3);
//		cb.append("new StringBuilder()");IndentSpace.newSpace(cb, 4);
//		cb.append(".append(field)");IndentSpace.newSpace(cb, 4);
//		cb.append(".append("+getCondition()+")");IndentSpace.newSpace(cb, 4);
//		cb.append(".toString(),");IndentSpace.newSpace(cb, 3);
//		
//		cb.append("new StringBuilder()");IndentSpace.newSpace(cb, 4);
//		cb.append(".append(\"%\")");IndentSpace.newSpace(cb, 4);
//		cb.append(".append("+FIRST_PARAMETER.getName()+")");IndentSpace.newSpace(cb, 4);
//		cb.append(".append(\"%\")");IndentSpace.newSpace(cb, 4);
//		cb.append(".toString(),");IndentSpace.newSpace(cb, 3);
//		cb.append("field");IndentSpace.newSpace(cb, 2);
//		
//		cb.append(")");
//		method.addBodyLine(cb.toString());
//		method.addBodyLine("return (T) this ");
		
		CharacterBuilder cb =CharacterBuilderFactory.createDefaultStringBuilder();
		cb.append(CriteriaConstant.ADD_CRITERIA_METHOD).append("(");IndentSpace.newSpace(cb, 3);
		cb.append("field,");IndentSpace.newSpace(cb, 3);
		cb.append(CriteriaConstant.TERM_CLASS_NAME).append(".").append(getCondition()).append(',');IndentSpace.newSpace(cb, 3);
		cb.append("new StringBuilder()");IndentSpace.newSpace(cb, 4);
		cb.append(".append(\"%\")");IndentSpace.newSpace(cb, 4);
		cb.append(".append("+FIRST_PARAMETER.getName()+")");IndentSpace.newSpace(cb, 4);
		cb.append(".append(\"%\")");IndentSpace.newSpace(cb, 4);
		cb.append(".toString()");IndentSpace.newSpace(cb, 3);
		cb.append(")");
		method.addBodyLine(cb.toString());
		method.addBodyLine("return (T) this ");
		
	}
	public static void main(String []args){
		AndLikeMethod  cm = new AndLikeMethod();
		System.out.println(cm.createMethod().compiledContent(0)); 
	}
	
	@Override
	protected String getCondition() {
		
		return CriteriaConstant.LIKE_FIELD_NAME;
	}
}
